﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.Contract;
using EFong.View.Proxy;

namespace EFong.View.FlightTicket
{
    public partial class Frm_FlightTicketReturn : BasicForm
    {
        private Proxy.BaseServiceProxy _baseProxy { get { return Proxy.BaseServiceProxy.GetInstance(); } }
        private Proxy.BusinessServiceProxy _businessProxy { get { return Proxy.BusinessServiceProxy.GetInstance(); } }
        private Proxy.MasterServiceProxy _masterProxy { get { return Proxy.MasterServiceProxy.GetInstance(); } }

        public Frm_FlightTicketReturn()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
            base.InitFields();
        }

        protected override void Events()
        {
            this.btn_OK.Click += (s, e) =>
            {
                save();
            };

            this.spe_CustomerReturnPrice.ValueChanged += (s, e) => { updateInteger(); updateCustomer实退费用(); };
            this.spe_CustomerUsedPrice.ValueChanged += (s, e) => { updateInteger(); updateCustomer实退费用(); };
            this.spe_UsedTax.ValueChanged += (s, e) => { updateInteger(); updateCustomer实退费用(); };

            this.spe_VendorReturnPrice.ValueChanged += (s,e) => {updateCustomer航空公司实退费用();};
            this.spe_VendorUsedPrice.ValueChanged += (s, e) => { updateCustomer航空公司实退费用(); };

            this.cmb_Vendor.EditValueChanged += (s, e) =>
            {
                Contract.RVendor vendor = this.cmb_Vendor.GetValue() as Contract.RVendor;

                if (vendor != null)
                {
                    SelVend = _masterProxy.GetVendorByID(vendor.ID).ReturnValue;
                }
                else
                {
                    SelVend = null;
                }
            };
        }

        public Contract.Vendor SelVend { get; set; }

        private void updateCustomer实退费用()
        {
            txt实退费用.SetValue(getDetail().CustomerReturnAmount);
        }

        private void updateCustomer航空公司实退费用()
        {
            txt航空公司实退费用.SetValue(getDetail().VendorReturnAmount);
        }

        protected override void LoadDatas()
        {
            this.cmb_OrderUser.Properties.BindingData(_baseProxy.GetEffUser().ReturnValue);
            this.bds_Vendor.BindingData(_masterProxy.GetAllFlightVendor().ReturnValue);
            this.cmb_BankAccount.Properties.BindingData(_masterProxy.GetAllEffectivedBankAccount());

            if (this.cmb_OrderUser.GetValue() == null)
                this.cmb_OrderUser.SetValue(AppParameters.LoginUser);
        }

        private void save()
        {
            UIOperManager.SaveData(
                () =>
                {
                    Contract.FlightReturn detail = getDetail();
                    UpdateToDetail(detail);
                    return _businessProxy.ReturnFlightOrder(Detail, Detail.FlightOrderDetail, Order.TotalProfit.Value).IsSuccess;
                },
                "退票成功.",
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                );
        }

        #region Detail
        private Contract.FlightReturn _detail;
        public Contract.FlightReturn Detail
        {
            get { return _detail; }
            set
            {
                _detail = value;
                onDetailChanged();
            }
        }

        public Contract.FlightOrder Order { get; set; }

        private void onDetailChanged()
        {
            if (Detail == null)
            {
                this.bds_flightWay.BindingData(typeof(Contract.FlightWay));
            }
            else
            {
                if (!string.IsNullOrEmpty(Detail.Ref))
                    this.txt_OrderRef.SetText(Detail.Ref);

                if (Detail.OrderUser != null)
                    this.cmb_OrderUser.SetText(Detail.OrderUser);

                this.dte_OrderDate.SetValue(Detail.OrderDate);

                this.spe_VendorUsedPrice.SetValue(Detail.VendorUsedPrice);
                this.spe_VendorReturnPrice.SetValue(Detail.VendorReturnPrice);
                txt航空公司实退费用.SetValue(Detail.VendorReturnAmount);

                this.spe_CustomerUsedPrice.SetValue(Detail.CustomerUsedPrice);
                this.spe_CustomerReturnPrice.SetValue(Detail.CustomerReturnPrice);
                this.spe_UsedTax.SetValue(Detail.UsedTax);
                txt实退费用.SetValue(getDetail().CustomerReturnAmount);
                txt_Remark.SetText(Detail.Remark);
                if (Detail.Vendor != null)
                {
                    RVendor vendor = new RVendor()
                    {
                        ID = Detail.Vendor.ID,
                        Name = Detail.Vendor.Name,
                    };
                    this.cmb_Vendor.SetValue(vendor);
                }
                SelVend = Detail.Vendor;

                this.bds_flightWay.BindingData(Detail.FlightOrderDetail.FlightWayList);
                this.cmb_PaymentType.SetDisplay(Detail.PaymentType);
                if (Detail.BankAccount != null)
                    this.cmb_BankAccount.SetValue(Detail.BankAccount);
            }
        }

        private Contract.FlightReturn getDetail()
        {
            Contract.FlightReturn detail = new Contract.FlightReturn();

            detail.OrderDate = dte_OrderDate.GetValue();
            detail.OrderUser = cmb_OrderUser.GetValue() as Contract.User;

            detail.VendorUsedPrice = spe_VendorUsedPrice.GetValue();
            detail.VendorReturnPrice = spe_VendorReturnPrice.GetValue();

            detail.CustomerUsedPrice = spe_CustomerUsedPrice.GetValue();
            detail.CustomerReturnPrice = spe_CustomerReturnPrice.GetValue();
            detail.UsedTax = spe_UsedTax.GetValue();

            detail.FlightOrderDetail = Detail.FlightOrderDetail;

            detail.Remark = txt_Remark.GetText();
            detail.Vendor = SelVend;
            detail.PaymentType = this.cmb_PaymentType.GetDisplay();
            detail.BankAccount = this.cmb_BankAccount.GetValue() as Contract.BankAccount;

            return detail;
        }

        private void UpdateToDetail(Contract.FlightReturn detail)
        {
            Detail.OrderUser = detail.OrderUser;
            Detail.OrderDate = detail.OrderDate;
            Detail.Remark = detail.Remark;
            Detail.VendorUsedPrice = detail.VendorUsedPrice;
            Detail.VendorReturnPrice = detail.VendorReturnPrice;
            Detail.CustomerUsedPrice = detail.CustomerUsedPrice;
            Detail.CustomerReturnPrice = detail.CustomerReturnPrice;
            Detail.UsedTax = detail.UsedTax;
            Detail.Vendor = detail.Vendor;
            Detail.BankAccount = detail.BankAccount;
            Detail.PaymentType = detail.PaymentType;
        }

        private void updateInteger()
        {
            spe_Integer.SetValue(getDetail().Intergral);
        }
        #endregion

        protected override void OnViewStateChanged()
        {
            switch (this.ViewState)
            {
                case ViewStates.BROWSER:
                    if (!AppParameters.IsAdRole)
                    {
                        this.btn_OK.Visible = false;
                        this.btn_OK.Enabled = false;
                        this.txt_Remark.ReadOnly(true);
                        this.spe_CustomerReturnPrice.ReadOnly(true);
                        this.spe_CustomerUsedPrice.ReadOnly(true);
                        this.spe_Integer.ReadOnly(true);
                        this.spe_UsedTax.ReadOnly(true);
                        this.spe_VendorReturnPrice.ReadOnly(true);
                        this.spe_VendorUsedPrice.ReadOnly(true);
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
